<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button
                v-if="row.type===1"
                size="small"
                type="text"
                @click="add(row.type+1,row.id)"
              >添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 添加或编辑的弹出框 -->
      <el-dialog :visible="showDialog" :title="showTilte" @close="handleClose">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { transListDataToTreeData } from '@/utils'
export default {
  name: 'Permissions',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    showTilte() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = transListDataToTreeData(data, '0')
      // console.log(this.list)

      // 1.将数据转换成树状的数据
      // 2.表格也要变成树状表格
    },
    handleClose() {
      this.showDialog = false
      this.formData = {
        enVisible: '0',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      }
    },
    // 添加权限
    add(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 提交权限表单
    async handleSubmit() {
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
        this.$message.success('更新权限成功')
      } else {
        await reqAddPermission(this.formData)
        this.$message.success('添加权限成功')
      }
      this.handleClose()
      this.getPermissionList()
    },

    // 删除权限
    async del(id) {
      try {
        await this.$confirm('你确定要删除该权限吗', '提标')
        await reqDelPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      } catch (err) {
        console.log(err)
      }
    },

    // 编辑
    async edit(id) {
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
    }
  }
}
</script>
